<?php

namespace Common\Bundle\LibraryBundle\Controller;

use Common\Bundle\LibraryBundle\Entity\Difficulte;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Common\Bundle\LibraryBundle\Form\DifficulteType;

/**
 * @Route("/difficulte")
 */
class DifficulteController extends Controller{

    /**
     * @Route("/", options={"expose"=true})
     * @Template()
     */
    public function indexAction()
    {
        $difficultes = $this->getDifficulteRepository()->findAll();
        return array("difficultes" => $difficultes);
    }

    /**
     * @Route("/new", options={"expose"=true}, defaults={"idDifficulte"=null})
     * @Route("/{idDifficulte}/edit", options={"expose"=true})
     * @Template()
     */
    public function editAction($idDifficulte)
    {
        if ($idDifficulte === null) {
            $difficulte = new Difficulte();
        } else {
            $difficulte = $this->getDifficulteRepository()->find($idDifficulte);
        }

        if (!$difficulte) {
            throw $this->createNotFoundException();
        }

        $form = $this->createForm(new DifficulteType(), $difficulte);

        if ($this->getRequest()->isMethod("post")) {
            $form->bind($this->getRequest());
            if ($form->isValid()) {
                $difficulte->setIntitule(ucfirst(mb_strtolower($difficulte->getIntitule(), "utf-8")));
                $this->getDoctrine()->getManager()->persist($difficulte);
                $this->getDoctrine()->getManager()->flush();
                if($idDifficulte){
                    $this->get('session')->getFlashBag()->add('success', 'Difficulté modifiée');
                }
                else
                {
                    $this->get('session')->getFlashBag()->add('success', 'Difficulté ajoutée');
                }
                return $this->redirect($this->generateUrl("common_library_difficulte_index", array()));
            }
        }

        return $this->render('CommonLibraryBundle:Difficulte:edit.html.twig', array(
            'form' => $form->createView(),
            'idDifficulte' => $idDifficulte
        ));
    }

    /**
     * @Route("/{idDifficulte}/delete", options={"expose"=true})
     */
    public function deleteAction($idDifficulte)
    {
        $difficulte = $this->getDifficulteRepository()->find($idDifficulte);
        $em = $this->getDoctrine()->getManager();

        $em->remove($difficulte);
        $em->flush();
        $this->get('session')->getFlashBag()->add('success', 'Difficulté supprimée');
        return $this->redirect($this->generateUrl("common_library_difficulte_index", array()));

    }


    /**
     * @return \Common\Bundle\LibraryBundle\Entity\Repository\DifficulteRepository
     */
    protected function getDifficulteRepository() {
        return $this->getDoctrine()->getRepository('CommonLibraryBundle:Difficulte');
    }

} 